import scrapy
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem


class ImgPipeline(ImagesPipeline):
    # 抓取的图片url获取一个Request用于下载
    def get_media_requests(self, item, info):
        yield scrapy.Request(item['image_url'])

# 下载完成后执行
    def item_completed(self, results, item, info):
        # 获取下载地址
        image_path = [x['path'] for ok, x in results if ok]
        if not image_path:
            raise DropItem("Item contains no images")
        # 将地址存入item
        item['image_path'] = image_path
        return item
